#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 14:43
from domain.report.common.period_enum import PeriodEnum
from domain.report.common.str_util import del_html_element
from domain.report.dto.cash_dto import CashDTO, CashSchema
from typing import List


_value_index_name = ["c_fr_sale_sg", "n_depos_incr_fi", "n_incr_loans_cb", "n_inc_borr_oth_fi", "prem_fr_orig_contr",
                     "n_reinsur_prem", "n_incr_insured_dep", "n_incr_disp_tfa", "ifc_cash_incr", "n_incr_loans_oth_bank",
                     "n_cap_incr_repur", "recp_tax_rends", "c_fr_oth_operate_a", "c_inf_fr_operate_a", "c_paid_goods_s",
                     "n_incr_clt_loan_adv", "n_incr_dep_cbob", "c_pay_claims_orig_inco", "pay_handling_chrg",
                     "pay_comm_insur_plcy", "c_paid_to_for_empl", "c_paid_for_taxes", "oth_cash_pay_oper_act",
                     "st_cash_out_act", "n_cashflow_act", "c_disp_withdrwl_invest", "c_recp_return_invest",
                     "n_recp_disp_fiolta", "n_recp_disp_sobu", "oth_recp_ral_inv_act", "stot_inflows_inv_act",
                     "c_pay_acq_const_fiolta", "c_paid_invest", "n_incr_pledge_loan", "n_disp_subs_oth_biz",
                     "oth_pay_ral_inv_act", "stot_out_inv_act", "n_cashflow_inv_act", "c_recp_cap_contrib",
                     "incl_cash_rec_saims", "c_recp_borrow", "proc_issue_bonds", "oth_cash_recp_ral_fnc_act",
                     "stot_cash_in_fnc_act", "c_prepay_amt_borr", "c_pay_dist_dpcp_int_exp",
                     "incl_dvd_profit_paid_sc_ms", "oth_cashpay_ral_fnc_act", "stot_cashout_fnc_act",
                     "n_cash_flows_fnc_act", "eff_fx_flu_cash", "n_incr_cash_cash_equ", "c_cash_equ_end_period"]

def xbrl2dtos(data, stock_code, period:PeriodEnum) -> List[CashDTO]:
    columns = data["columns"]
    rows = data["rows"]
    dto_list = list()
    for i in range(len(columns)):
        dto = CashDTO()
        dto.stock_code:str = stock_code
        dto.stock_id = int(stock_code.split(".")[0])
        dto.period = period
        dto.year = int(del_html_element(data["columns"][i]["title"]))
        for j in range(len(data["rows"])):
            val = del_html_element(rows[j][del_html_element(columns[i]["field"])])
            val = val.replace("-", "") if val and "-" in val else val
            CashSchema.set(dto, _value_index_name[j], val)
        dto_list.append(dto)
    return dto_list
